class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        if(l1 == nullptr||l2 == nullptr)
        {
            return l1 == nullptr? l2:l1;
        }
        //模拟加法，使用一个cnt表示是否需要进位即可解决这道题目
        int cnt = 0;
        ListNode* cur1 = l1;
        ListNode* cur2 = l2;
        ListNode* newhead = new ListNode(1);
        ListNode* cur = newhead;
        while(cur1&&cur2)
        {
            int tmp = cur1->val+cur2->val+cnt;
            cnt = tmp/10;//看是否存在进位
            tmp = tmp%10;//不能存在大于10的值
            cur->next = new ListNode(tmp);
            cur = cur->next;
            cur1 = cur1->next;
            cur2 = cur2->next;
        }
        while(cur1)
        {
            int tmp = cur1->val+cnt;
            cnt = tmp/10;
            tmp = tmp%10;
            cur->next = new ListNode(tmp);
            cur = cur->next;
            cur1 = cur1->next;
        }
        while(cur2)
        {
            int tmp = cur2->val+cnt;
            cnt = tmp/10;
            tmp = tmp%10;
            cur->next = new ListNode(tmp);
            cur = cur->next;
            cur2 = cur2->next;
        }
        if(cnt!=0)
        {
            cur->next = new ListNode(cnt);
        }
        return newhead->next;
    }
};